import {
  downloadViolationImage,
  getAllViolationType,
  getDriverRecordList,
  getViolationImage,
  getViolationRecordList
} from "../../../../api/data";

export default {
  name: "violate-component",
  data() {
    return {
      name: '',
      modal1: false,
      imgSrc: '',
      queryData: {
        start: '',
        end: '',
        name: '',
        type: '7',
        account: '',
        page: '1',
        limit: '10'
      },
      violateType: [],
      columns1: [
        {
          title: '编号',
          key: 'udvrecordId'
        },
        {
          title: '姓名',
          key: 'name'
        },
        {
          title: '违规行为',
          key: 'udvtypeName'
        },
        {
          title: '时间',
          key: 'createTime'
        },
        {
          title: '地址',
          key: 'udvrecordLocation'
        },
        {
          title: '操作',
          key: 'action',
          slot: 'action',
          align: 'center'
        }
      ],
      data1: []
    }
  },
  methods: {
    changePage(pageNumber) {
      this.queryData.page = pageNumber
      this.queryList()
    },
    changeSize(pageSize) {
      this.queryData.limit = pageSize
    },
    queryList() {
      getViolationRecordList(this.queryData).then(res => {
        this.data1 = res.data.obj
        this.data1.forEach(item => {
          item.createTime = item.createTime[0] + "-" + item.createTime[1] + "-" + item.createTime[2] + " " + item.createTime[3] + ":" + item.createTime[4] + ":" + item.createTime[5]
        })
      }).catch(err => {
      })
    },
    getAllViolationType_() {
      getAllViolationType().then(res => {
        this.violateType = res.data.obj
      }).catch(err => {
      })
    },
    detail(index) {
      this.selected = index
      getViolationImage(this.data1[index].udvrecordId).then(res => {
        this.imgSrc = 'data:image/jpeg;base64,' + res.data.obj
      }).catch(err => {
        alert(err.message())
      })
      this.modal1 = true
    },
    download(index) {
      this.selected = index
      getViolationImage(this.data1[index].udvrecordId).then(res => {
        let context = 'data:image/jpeg;base64,' + res.data.obj
        this.downloadFileByBase64(context,'违规记录图片'+this.data1[index].udvrecordId+'.jpg')
        this.$Message.info("下载成功")
      }).catch(err => {
        alert(err.message())
      })
    },
// 下载Base64文件
    downloadFileByBase64(data, fileName) {
      const blob = this.base64ToBlob(data);
      const url = URL.createObjectURL(blob);
      this.downloadFile(url, fileName)
    },

// base64转blob
    base64ToBlob(data) {
      let arr = data.split(',');
      let mime = arr[0].match(/:(.*?);/)[1];
      let bStr = window.atob(arr[1]);
      let n = bStr.length;
      let u8arr = new Uint8Array(n);

      while (n--) {
        u8arr[n] = bStr.charCodeAt(n);
      }
      return new Blob([u8arr], {type: mime});
    },

// 下载
    downloadFile(url, fileName) {
      const a = document.createElement("a");
      a.setAttribute("href", url)
      a.setAttribute("download", fileName)
      a.setAttribute("target", "_blank")
      let clickEvent = document.createEvent("MouseEvents");
      clickEvent.initEvent("click", true, true);
      a.dispatchEvent(clickEvent);
    },
    ok() {

    },
    click() {

    },
    reset() {

    }
  },
  mounted() {
    this.getAllViolationType_()
  }
}
